import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.common.Term;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.UpdateOptions;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.bson.Document;
import org.bson.types.ObjectId;

import java.io.*;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

public class Main_2021_07_21 {
    public static void main(String[] args) throws IOException, InvalidFormatException {
        HashSet<String> positiveSet = Test.ReadWord("poative.txt");
        HashSet<String> negativeSet = Test.ReadWord("negative.txt");
        int count = 0;

        Workbook workbook = WorkbookFactory.create(new File("D:\\测试临时文件\\待分析_业绩说明会问答文本分析.xlsx"));
        Sheet sheet = workbook.getSheetAt(0);

        final int ROWSIZE = sheet.getPhysicalNumberOfRows();
        for (int i = 1; i < ROWSIZE; i++) {
            System.out.println("解析第" + (i+1) + "行...");
            Row row = sheet.getRow(i);

            String huiDaInfo = row.getCell(7) + "";

            if (!huiDaInfo.equals("") || !huiDaInfo.equals("null")){
                int posNum = CountWord(huiDaInfo,positiveSet);
                int negNum = CountWord(huiDaInfo,negativeSet);
                row.getCell(8).setCellValue(posNum);
                row.getCell(9).setCellValue(negNum);

                StringBuilder resultBuilder = new StringBuilder();
                List<Term> termList = HanLP.segment(huiDaInfo);
                for(Term term:termList){
                    if(!StopWords.contains(term.word)){
                        resultBuilder.append(term.word+" ");
                    }
                }
                row.createCell(13).setCellValue(resultBuilder.toString());

                int total = resultBuilder.toString().split(" ").length;
                float pospct = (float)posNum/total;
                float negpct = (float)negNum/total;
                if((pospct+negpct)>0){
                    float tone = (pospct-negpct)/(pospct+negpct);
                    row.getCell(12).setCellValue(tone);
                }
                row.getCell(10).setCellValue(pospct);
                row.getCell(11).setCellValue(negpct);
            }
        }

        System.out.println("解析结束");
        workbook.write(new FileOutputStream("业绩说明会问答文本分析.xlsx"));

    }

    public static int CountWord(String line, HashSet<String> set){
        Iterator<String> iter = set.iterator();
        int total = 0;
        while (iter.hasNext()){
            String word = iter.next();
            if(line.contains(word)){
                total = total+WordNum(word,line);
            }
        }
//        System.out.println("--------------");
        return total;
    }

    public static int WordNum(String word, String line){
        int len1 = line.length();
        line = line.replace(word,"");
        int len2 = line.length();
//        System.out.println(len1-len2);
//        System.out.println(word.length());
        return (len1-len2)/word.length();
    }

    public static HashSet<String> ReadWord(String dir){
        HashSet<String> set = new HashSet<String>();
        BufferedReader bfr = null;
        try {
            bfr = new BufferedReader(new FileReader(new File(dir)));
            String s;
            while ((s = bfr.readLine())!=null){
                if(s.trim().length()>0&&s.length()>1){
                    set.add(s);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bfr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return set;
    }
}
